using System;
using System.Collections.Generic;
using System.Data;
using System.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace CoolStuffs.Sharepoint.Webparts.RecentItems
{
    /// <summary>
    /// Actual Document Aggregator Webpart
    /// This Webpart aggregates the items of
    /// Document Content Type
    /// </summary>
    public sealed class DocumentsAggregator : BaseContentAggregator
    {
        #region Nested Enums/Structures/Classes

        #region Public
        #endregion

        #region Protected
        #endregion

        #region Internal
        #endregion

        #region Private
        #endregion

        #endregion

        #region Member Variables

        #region Public
        #endregion

        #region Protected
        #endregion

        #region Internal
        #endregion

        #region Private
        private System.Data.DataTable _recentItemsDataTable;
        private System.String _fileRefField = "FileRef";
        private System.String _fileLeafRefField = "FileLeafRef";
        private System.String _nameField = "Name";
        private const System.String _oneInTheEnd = "1";
        private const System.String _slash = "/";
        #endregion

        #endregion

        #region Constructors/Destructors

        #region Public
        /// <summary>
        /// Default Constructor
        /// </summary>
        public DocumentsAggregator()
            : base()
        {
            this.DocumentType = DocumentBaseTypeScope.DocumentLibrary;
        }
        #endregion

        #region Protected
        #endregion

        #region Internal
        #endregion

        #region Private
        #endregion

        #endregion

        #region Properties

        #region Public
        #endregion

        #region Protected
        #endregion

        #region Internal
        #endregion

        #region Private
        #endregion

        #endregion

        #region Methods

        #region Public
        #endregion

        #region Protected
        /// <summary>
        /// CreateChildControls Event of System.Web.UI.WebControls.Webparts.Webpart
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.ContentTypeSpecificAction();
            string[] fields = { this._fileRefField };
            this.InsertItemNameFieldInGrid(fields, this._fileLeafRefField, this._nameField);
            this.BindGrid();
        }

        /// <summary>
        /// This is a Overriden Method of
        /// the base class
        /// </summary>
        protected override void ContentTypeSpecificAction()
        {
            this.FieldsInDocumentType[this.DocumentType].Add(this._fileRefField);
            this.FieldsInDocumentType[this.DocumentType].Add(this._fileLeafRefField);
            this.GetRecentItemsDataView(this._fileLeafRefField);
            DataTable _tempTable = new DataTable();
            this._recentItemsDataTable = this.RecentItemsDataView.ToTable();
            _tempTable = this.RecentItemsDataView.ToTable();
            /// We are removing version trackers and hidden items
            /// related to some internal tracking, which we dont require
            this._recentItemsDataTable = _tempTable.Clone();
            this._recentItemsDataTable.Clear();
            foreach (DataRow _eachRow in _tempTable.Rows)
            {
                bool _unwantedHiddenItems = false;
                string _row = _eachRow[this._fileLeafRefField].ToString();
                string _annoyingOneBitItem = _row.Substring(_row.IndexOf(this.Hash) + 1);
                _unwantedHiddenItems = _annoyingOneBitItem.EndsWith(_oneInTheEnd);
                if (!_unwantedHiddenItems)
                {
                    _recentItemsDataTable.Rows.Add(_eachRow.ItemArray);
                }
            }
            /// tailoring column data for our need to get Document Name
            /// as well as its fully qualified URL
            foreach (DataRow _currentRow in _recentItemsDataTable.Rows)
            {
                string _fileLeafRefData = _currentRow[this._fileLeafRefField].ToString();
                _currentRow[this._fileLeafRefField] = _fileLeafRefData.
                    Substring(_fileLeafRefData.IndexOf(this.Hash) + 1);
                _fileLeafRefData = _currentRow[this._fileRefField].ToString();
                _currentRow[this._fileRefField] = _fileLeafRefData.Substring(_fileLeafRefData.IndexOf(this.Hash) + 1);
                _currentRow[this._fileRefField] = Page.Request.Url.GetLeftPart(UriPartial.Authority) +
                                                    _slash + _currentRow[this._fileRefField];
                _currentRow.AcceptChanges();
            }
            /// finally, get the updated changes back in our final view
            this.RecentItemsDataView = new DataView(this._recentItemsDataTable);
        }
        #endregion

        #region Internal
        #endregion

        #region Private
        #endregion

        #endregion
    }
}
